package presentation;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;

import db.Album;
import db.DbUtils;
import db.Picture;


 public class EditAlbumServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	 private static final long serialVersionUID = 1L;

		protected void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			
			request.setCharacterEncoding("UTF-8");
			Long albumId = Long.parseLong(request.getParameter("album_id"));
			String newAlbumName = request.getParameter("albumName");
			Session hbSession = DbUtils.startSession();
			if (newAlbumName == null) {
				response.sendRedirect("Edit.jsp?album_id=" + albumId);
				HttpSession session = request.getSession();
				Album album=DbUtils.getAlbumbyId(hbSession, albumId);
				session.setAttribute("oldAlbumName", album.getAlbumName());

			} else {
				try {
					DbUtils.editAlbum(hbSession, newAlbumName, albumId);
					hbSession.getTransaction().commit();
					response.sendRedirect("MyAlbum.jsp?root=true");
				} catch (Exception e) {
					if (hbSession != null) {
						hbSession.getTransaction().rollback();
						hbSession.close();
					}
					response.sendRedirect("Error.jsp?msg=pic");
					e.printStackTrace();
				}
			}
		}

		@Override
		protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
				throws ServletException, IOException {
			doPost(arg0, arg1);
		}  	  	    
}